﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;

namespace SES.CMS
{
    public partial class Article : System.Web.UI.Page
    {
        cmsArticleBL artBL = new cmsArticleBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                int ArticleID = int.Parse(Request.QueryString["ID"].ToString());
                InitForm(ArticleID);
            }
        }

        private void InitForm(int ArticleID)
        {
            DataTable dtArticle = artBL.SelectByArticleID(ArticleID);

            ltlArtTitle.Text = dtArticle.Rows[0]["Title"].ToString();
            ltlContent.Text = dtArticle.Rows[0]["ArticleDetail"].ToString();

            lblCateTitle.Text = new cmsCategoryBL().Select(new cmsCategoryDO { CategoryID = int.Parse(dtArticle.Rows[0]["CategoryID"].ToString()) }).Title;

            int STT = int.Parse(dtArticle.Rows[0]["STT"].ToString());


            DataTable dtNext = artBL.SelectPreviousNextArticle(STT, 1);
            if (dtNext.Rows.Count > 0)
            {
                hplNext.Text = "Next: " + WordCut(dtNext.Rows[0]["Title"].ToString());
                hplNext.NavigateUrl = "/Article/" + int.Parse(dtNext.Rows[0]["ArticleID"].ToString()) + "/" + FriendlyUrl(dtNext.Rows[0]["Title"].ToString());
            }
            DataTable dtPre = artBL.SelectPreviousNextArticle(STT, -1);
            if (dtPre.Rows.Count > 0)
            {
                hplPrevious.Text = "Previous: " + WordCut(dtPre.Rows[0]["Title"].ToString());
                hplPrevious.NavigateUrl = "/Article/" + int.Parse(dtPre.Rows[0]["ArticleID"].ToString()) + "/" + FriendlyUrl(dtPre.Rows[0]["Title"].ToString());
            }


        }

        public string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
        public string WordCut(string s)
        {
            return Ultility.WordCut(s, 20, new char[] { ' ', '.', ',', ';' }) + "...";
        }
    }
}
